package com.wgke.bluetooth.ui.fragment;

import android.content.Intent;
import android.graphics.PointF;
import android.opengl.GLSurfaceView;
import android.view.View;

import com.wgke.bluetooth.R;
import com.wgke.bluetooth.ui.HeatMapActivity;
import com.wgke.bluetooth.utils.LogUtil;
import com.wgke.bluetooth.view.PressureMatrixRenderer;
import com.wgke.bluetooth.view.PressureMatrixRenderer2;
import com.wgke.bluetooth.view.PressureMatrixView;
import com.wgke.bluetooth.view.PressureRenderer;
import com.wgke.bluetooth.vm.BaseVM;

public class HeatMap2DFragment extends BaseFragment {

    private PressureMatrixView glSurfaceView;
    private PressureMatrixRenderer2 renderer;

    @Override
    public int getLayoutId() {
        return R.layout.layout_2d;
    }

    @Override
    protected void initView(View rootView) {
        //rootView.findViewById(R.id.llType).setVisibility(View.GONE);
        rootView.findViewById(R.id.toggleViewButton).setOnClickListener(v -> {
            HeatMapActivity.start(context, 1);
        });
        rootView.findViewById(R.id.toggleViewButton2).setOnClickListener(v -> {
            startAnimation();
        });
        glSurfaceView = rootView.findViewById(R.id.glSurfaceView);
        renderer = new PressureMatrixRenderer2();
        renderer.WIDTH = glSurfaceView.MATRIX_WIDTH;
        renderer.HEIGHT = glSurfaceView.MATRIX_HEIGHT;
        glSurfaceView.setPressureData(renderer.initTestData());


//        glSurfaceView.setEGLContextClientVersion(2);
//        // 创建渲染器
//        renderer = new PressureMatrixRenderer2();
//        glSurfaceView.setRenderer(renderer);
//        // 设置渲染模式为按需渲染，提高性能
//        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
        BaseVM vm = new BaseVM();
        vm.createBtn(rootView.findViewById(R.id.layout_total1), "单元格：", 10, 1, v -> {
            int value = (Integer) v.getTag();
            glSurfaceView.setCellSize(value);
        });
        vm.createBtn(rootView.findViewById(R.id.layout_total2), "间隔：", 3, 1, v -> {
            int value = (Integer) v.getTag();
            glSurfaceView.setGapSize(value);
        });
        //rootView.findViewById(R.id.layout_total2).setVisibility(View.GONE);
        rootView.findViewById(R.id.layout_total3).setVisibility(View.GONE);
        rootView.findViewById(R.id.layout_total4).setVisibility(View.GONE);
        rootView.findViewById(R.id.layout_total5).setVisibility(View.GONE);
        rootView.findViewById(R.id.layout_total6).setVisibility(View.GONE);

    }

    Thread thread = null;
    int count = 0;

    void startAnimation() {

        if (thread == null) {
            thread = new Thread(() -> {
                while (true) {
                    if (getActivity().isFinishing()) {
                        return;
                    }
                    try {
                        Thread.sleep(30L);
                        PointF point = new PointF(100f, 75f); // 圆心 (50, 33)
                        float radius = 20f; // 半径15单位
                        float angleDegrees = count * 1f; // 角度5度
                        PointF pointOnCircle = getPointOnCircle(point, radius, angleDegrees);
                        getActivity().runOnUiThread(() -> {
                            LogUtil.d("2D", "" + (int) pointOnCircle.x + ";Y=" + (int) pointOnCircle.y);
                            renderer.initTestData((int) pointOnCircle.x, (int) pointOnCircle.y);
                        });
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count++;
                }
            });
            thread.start();
        }
    }

    public PointF getPointOnCircle(PointF center, float radius, float angleDegrees) {
        float angleRadians = (float) Math.toRadians(angleDegrees);
        float x = center.x + radius * (float) Math.cos(angleRadians);
        float y = center.y + radius * (float) Math.sin(angleRadians);
        return new PointF(x, y);
    }

    @Override
    public void onResume() {
        super.onResume();
        //glSurfaceView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        // glSurfaceView.onPause();
    }

}
